package com.algorithm.leetcode.SumTwoNums;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author ADMIN
 * @date 2021/7/27 23:39
 */
public class TwoNumsSum {
    public static void main(String[] args) {
        int[] ints = {1,2,3,4,56,6};

//        int[] ints1 = get(ints, 62);
        int[] ints1 = getTarget(ints, 62);
        System.out.println(Arrays.toString(ints1));
    }

    public static int[] get(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int i1 = i+1; i1 < nums.length ; i1++) {
                if (nums[i] + nums[i1] == target) {
                    int[] ints = {i, i1};
                    return ints;
                }
            }
        }
        return null;
    }
    public static int[] getTarget(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])) {
                return new int[]{map.get(target-nums[i]),i};
            }
            map.put(nums[i], i);
        }
        return null;
    }
}
